//
//  LHSingerDetailViewController.m
//  Enjoy_Music
//
//  Created by Bruce_Lin on 15/6/11.
//  Copyright (c) 2015年 Bruce. All rights reserved.
//

#import "LHSingerDetailViewController.h"
#import <UIImageView+WebCache.h>
#import "MyNavgationView.h"
#import "LHSingerDetailModel.h"
#import "LHSingerDetailURLModel.h"
#import "LHSingerDetailTableViewCell.h"


#define KSingerDetailCell @"singerDetailReuse"
@interface LHSingerDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic ,strong)UITableView *listTableView;
@property (nonatomic ,strong)NSMutableArray *songListArr;
@property (nonatomic ,strong)NSMutableArray *songDetailArr;
@property (nonatomic ,strong)NSMutableArray *songFinalArr;
@property (nonatomic ,strong)NSMutableArray *songIdArr;
@property (nonatomic ,copy)NSMutableString *requestID;
@property (nonatomic ,copy)NSString *picStr;
@property (nonatomic ,strong)MyNavgationView *navi;

@end

@implementation LHSingerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置状态栏颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    self.songListArr = [NSMutableArray array]; //存放 LHNewDetailModel对象(songlist , pics)
    self.songDetailArr = [NSMutableArray array]; //存放 LHNewSongModel对象(singer_name, song_name, LHSongDetailModel *songDetailModel)
    self.requestID = [NSMutableString string];
    self.songFinalArr = [NSMutableArray array];  //存放数组 数组中存放每首歌曲的3种格式信息
    [self.view addSubview:self.listTableView];
    
    [self creatNavigation];
    [self getSongListData];
}

- (void)creatNavigation
{
    //隐藏系统导航栏
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navi = [[MyNavgationView alloc]initWithFrame:CGRectMake(0, 0, LHWidth, 64)];
    [self.view addSubview:_navi];
    [_navi creatTitle:@"歌曲列表"];
    [_navi creatLeftButtonWithImage:@"btn_nav_back" target:self action:@selector(back)];
    
    
    //更新状态栏颜色
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  网络请求
 */
- (void)getSongListData
{
    NSString *path = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@?detail=true&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.2&active=1&net=2&openudid=7a702b310d3c0910aca4228a673a841751162b63&idfa=E1922CE5-ECA3-494B-9B6A-B637D35AFDC2&utdid=VXEJsNE34hwDAGokQcEmljVB&alf=201200&bundle_id=com.ttpod.music",self.singerID];
    [LHHttpHandler get:path body:nil result:LHHttp success:^(id result) {
        
#ifdef IOS_DEBUG
        NSLog(@"%@",result);
#endif
        self.picStr = [[result objectForKey:@"data"] objectForKey:@"picUrl"];  //顶部大图网址
        
        [self getSongData];
        
    } failure:^(NSError *error) {
        
#ifdef IOS_DEBUG
        NSLog(@"%@",error);
#endif
        
    }];
}

- (void)getSongData
{
    NSString *path = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=1&size=100&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.2&active=1&net=2&openudid=7a702b310d3c0910aca4228a673a841751162b63&idfa=E1922CE5-ECA3-494B-9B6A-B637D35AFDC2&utdid=VXEJsNE34hwDAGokQcEmljVB&alf=201200&bundle_id=com.ttpod.music",self.singerID];
    
    [LHHttpHandler get:path body:nil result:LHHttp success:^(id result) {
#ifdef IOS_DEBUG
        NSLog(@"%@",result);
        
        for (NSDictionary *dic in [result objectForKey:@"data"]) {
            
            LHSingerDetailModel *model = [[LHSingerDetailModel alloc]initWithDic:dic];
            
            [_songDetailArr addObject:model];
        }
        
        for (LHSingerDetailModel *tempModel in _songDetailArr) {
            
            NSMutableArray *arr = [NSMutableArray array];
            
            for (NSDictionary *dic in [tempModel urlList]) {
                
                LHSingerDetailURLModel *model = [[LHSingerDetailURLModel alloc]initWithDic:dic];
                
                [arr addObject:model];
            }
            
            [_songFinalArr addObject:arr];
        }
        
        [_listTableView reloadData];
        
        
#endif
    } failure:^(NSError *error) {
#ifdef IOS_DEBUG
        NSLog(@"%@",error);
#endif
    }];
}

/**
 *  listTableView懒加载
 *
 *  @return listTableView
 */
- (UITableView *)listTableView
{
    if (!_listTableView) {
        self.listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, LHWidth, LHHeight - 64) style:UITableViewStyleGrouped];
        _listTableView.dataSource = self;
        _listTableView.delegate = self;
        UIImageView *ima = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"back1"]];
        [_listTableView setBackgroundView:ima];
        [_listTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_listTableView registerClass:[LHSingerDetailTableViewCell class] forCellReuseIdentifier:KSingerDetailCell];
    }
    return _listTableView;
}

#pragma mark - listTableView协议方法


//头部添加轮播图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImageView *view = [[UIImageView alloc]init];
    [view sd_setImageWithURL:[NSURL URLWithString:_picStr] placeholderImage:nil];
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 250;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _songDetailArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHSingerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KSingerDetailCell];
    cell.titleNameLabel.text = [_songDetailArr[indexPath.row] singerName];
    cell.singerNameLabel.text = [_songDetailArr[indexPath.row]name];
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return LHHeight / 9;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
